package lambdasinaction.chap6;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.*;
import static lambdasinaction.chap6.Dish.menu;

public class Partitioning {

    public static void main(String... args) {
//        System.out.println("Dishes partitioned by vegetarian: " + partitionByVegeterian());
//        System.out.println("Vegetarian Dishes by type: " + vegetarianDishesByType());
//        System.out.println("Most caloric dishes by vegetarian: " + mostCaloricPartitionedByVegetarian());
//        System.out.println("Most caloric dishes by vegetarian: " + test1());
//        System.out.println((IntStream.range(500, 1000).boxed().collect(Collectors.toList())));
        Random random = new Random();
        List list = random.ints(100, 500).limit(10).boxed().sorted(((o1, o2) -> {
            if (o1>o2 ) return -1;
            else return 1;
        })).collect(toList());
        System.out.println(list);
    }

    private static Map<Boolean, List<Dish>> partitionByVegeterian() {
        return menu.stream().collect(partitioningBy(Dish::isVegetarian));
    }

    private static Map<Boolean, Map<Dish.Type, List<Dish>>> vegetarianDishesByType() {
        return menu.stream().collect(partitioningBy(Dish::isVegetarian, groupingBy(Dish::getType)));
    }

    private static Object mostCaloricPartitionedByVegetarian() {
        return menu.stream().collect(
                partitioningBy(Dish::isVegetarian,
                        collectingAndThen(
                                maxBy(comparingInt(Dish::getCalories)),
                                Optional::get)));
    }
//    private Map Object test1() {
//        return menu.stream().collect(
//                partitioningBy(Dish::isVegetarian,
//                        partitioningBy(d-> d.getCalories()>500)));
//    }
}

